using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using NNS.Framework;
using QLTB.BusinessObjects;

namespace QLTB.DataAccess
{
    /// <summary>
    /// Project: NNS System 
    /// Generated Class for Table : TBL_lichsuDA.
    /// Date: 19/07/2010
    /// Author: Trần Hoàng Tâm
    /// </summary>
    public class TBL_lichsuDA : Object
    {
        #region NewID

        public string NewID(string key)
        {
            return SqlHelperSmart.GenCode("TBL_lichsu", "Code", key, 6);
        }

        #endregion

        #region ExistBy

        public bool ExistById(
            int id)
        {
            bool Result = false;
            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader(CommandType.StoredProcedure, "TBL_lichsu_Exist",
                                                                    new[] {new SqlParameter("@id", id)});
            if (myReader != null)
            {
                Result = myReader.HasRows;
                myReader.Close();
                myReader.Dispose();
            }
            return Result;
        }

        #endregion

        #region GetBy

        public TBL_lichsu GetById(
            int id)
        {
            TBL_lichsu obj = null;
            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader("TBL_lichsu_Get",
                                                                    new[] {new SqlParameter("@id", id)});
            if (myReader != null)
            {
                if (myReader.Read())
                {
                    obj = Populate(myReader);
                }
                myReader.Close();
                myReader.Dispose();
            }
            return obj;
        }

        #endregion

        #region Populate

        public TBL_lichsu Populate(
            IDataReader myReader)
        {
            TBL_lichsu obj = null;
            if (myReader != null)
            {
                obj = new TBL_lichsu();
                if (!myReader.IsDBNull(myReader.GetOrdinal("id")))
                {
                    obj.id = Convert.ToInt32(myReader["id"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("idthietbi")))
                {
                    obj.idthietbi = Convert.ToInt32(myReader["idthietbi"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("ngay")))
                {
                    obj.ngay = Convert.ToDateTime(myReader["ngay"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("noidung")))
                {
                    obj.noidung = Convert.ToString(myReader["noidung"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("nguoiTH")))
                {
                    obj.nguoiTH = Convert.ToString(myReader["nguoiTH"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("idNhanvien")))
                {
                    obj.idNhanvien = Convert.ToInt32(myReader["idNhanvien"]);
                }
            }
            return obj;
        }

        #endregion

        #region Add

        public int Add(
            int idthietbi,
            DateTime ngay,
            string noidung,
            string nguoiTH,
            int idNhanvien)
        {
            return Add(new TBL_lichsu
                           {
                               idthietbi = idthietbi,
                               ngay = ngay,
                               noidung = noidung,
                               nguoiTH = nguoiTH,
                               idNhanvien = idNhanvien
                           });
        }

        public int Add
            (
            TBL_lichsu obj)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_lichsu_Insert",
                                                    new[]
                                                        {
                                                            new SqlParameter("idthietbi", obj.idthietbi),
                                                            new SqlParameter("ngay", obj.ngay),
                                                            new SqlParameter("noidung", obj.noidung),
                                                            new SqlParameter("nguoiTH", obj.nguoiTH),
                                                            new SqlParameter("idNhanvien", obj.idNhanvien),
                                                        });
        }

        public int Duplicate(
            string id)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_lichsu_Duplicate",
                                                    new[] {new SqlParameter("@id", id)});
        }

        #endregion

        #region Update

        public int Update(
            int id,
            int idthietbi,
            DateTime ngay,
            string noidung,
            string nguoiTH,
            int idNhanvien)
        {
            return Update(new TBL_lichsu
                              {
                                  id = id,
                                  idthietbi = idthietbi,
                                  ngay = ngay,
                                  noidung = noidung,
                                  nguoiTH = nguoiTH,
                                  idNhanvien = idNhanvien
                              });
        }

        public int Update
            (TBL_lichsu obj)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_lichsu_Update",
                                                    new[]
                                                        {
                                                            new SqlParameter("id", obj.id),
                                                            new SqlParameter("idthietbi", obj.idthietbi),
                                                            new SqlParameter("ngay", obj.ngay),
                                                            new SqlParameter("noidung", obj.noidung),
                                                            new SqlParameter("nguoiTH", obj.nguoiTH),
                                                            new SqlParameter("idNhanvien", obj.idNhanvien),
                                                        });
        }

        #endregion

        #region Delete

        public int Delete(
            int id)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_lichsu_Delete", new[]
                                                                                                          {
                                                                                                              new SqlParameter
                                                                                                                  ("@id",
                                                                                                                   id),
                                                                                                          });
        }

        public int Destroy(
            string id)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_lichsu_DestroyBy",
                                                    new[] {new SqlParameter("@id", id)});
        }

        public int Destroy()
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_lichsu_Destroy");
        }

        #endregion

        #region GetDataSet 

        public DataSet GetDataSet()
        {
            return SqlHelper.Helper.ExecuteDataSet("TBL_lichsu_GetList");
        }

        #endregion

        #region GetDataTable 

        public DataTable GetDataTable()
        {
            return SqlHelper.Helper.ExecuteDataTable("TBL_lichsu_GetList");
        }

        #endregion

        #region GetList 

        public List<TBL_lichsu> GetList()
        {
            using (
                IDataReader reader = SqlHelper.Helper.ExecuteReader(CommandType.StoredProcedure, "TBL_lichsu_GetList"))
            {
                var list = new List<TBL_lichsu>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        #endregion

        #region Count

        public int Count()
        {
            return (int) SqlHelper.Helper.ExecuteScalar(CommandType.StoredProcedure, "TBL_lichsu_Count");
        }

        #endregion Count
    }
}